package io.sdl.common.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.sdl.common.constant.UrlConstants;
import io.sdl.common.service.impl.CrudServiceImpl;
import io.sdl.common.sys.dto.SdlInstitutionQaqDTO;
import io.sdl.common.utils.ConvertUtils;
import io.sdl.common.utils.Query;
import io.sdl.common.sys.dao.SdlInstitutionQaqDao;
import io.sdl.common.sys.entity.SdlInstitutionQaqEntity;
import io.sdl.common.sys.service.SdlInstitutionQaqService;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author Dc 1211734836@qq.com
 * @since 1.0.0 2025-03-28
 */
@Service
public class SdlInstitutionQaqServiceImpl extends CrudServiceImpl<SdlInstitutionQaqDao, SdlInstitutionQaqEntity, SdlInstitutionQaqDTO> implements SdlInstitutionQaqService {


    @Override
    public QueryWrapper<SdlInstitutionQaqEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<SdlInstitutionQaqEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public Integer getNum(Query query) {
        return baseDao.getNum(query);
    }

    /*
        获取机构资质列表
         */
    @Override
    public List<SdlInstitutionQaqDTO> getQaqListByInstitutionId(Long institutionId) {

        List<SdlInstitutionQaqEntity> qaqEntityList = baseDao.getQaqListByInstitutionId(institutionId);
        List<SdlInstitutionQaqDTO> sdlInstitutionQaqDTOS = ConvertUtils.sourceToTarget(qaqEntityList, SdlInstitutionQaqDTO.class);
        sdlInstitutionQaqDTOS.forEach(e->{
            ArrayList<String> url = new ArrayList<>();
            Arrays.stream(e.getCertificateFile().split(",")).toList().forEach(certificateFile->{
                url.add(UrlConstants.getProductionUrl() + certificateFile);
            });
            e.setCertificateFilesList(url);
        });
        return  sdlInstitutionQaqDTOS;
    }

    //获取机构的资质
    @Override
    public List<SdlInstitutionQaqDTO> getManageByInstitutionId(Query query) {

        query.put("page",query.getPage()-1);
        List<SdlInstitutionQaqEntity> manageByInstitution = baseDao.getManageByInstitutionId(query);
        List<SdlInstitutionQaqDTO> sdlInstitutionQaqDTOS = ConvertUtils.sourceToTarget(manageByInstitution, SdlInstitutionQaqDTO.class);
        sdlInstitutionQaqDTOS.forEach(e->{
            ArrayList<String> url = new ArrayList<>();
            Arrays.stream(e.getCertificateFile().split(",")).toList().forEach(certificateFile->{
                url.add(UrlConstants.getProductionUrl()+ certificateFile);
            });
            e.setCertificateFilesList(url);
        });
        return sdlInstitutionQaqDTOS ;

    }

    @Override
    public int submitExamine(SdlInstitutionQaqDTO dto) {
        SdlInstitutionQaqEntity entity = ConvertUtils.sourceToTarget(dto, SdlInstitutionQaqEntity.class);
        return baseDao.submitExamine(entity);
    }


    /*
   获取所有待审核的资质
    */
//    @Override
//    public PageInfo<SdlInstitutionQaqEntity> getList(Map<String, Object> params) {
//        Integer pageNum = Integer.valueOf(params.get("pagenum").toString());
//        Integer pageSize = Integer.valueOf(params.get("pagesize").toString());
//
//        PageHelper.startPage(pageNum,pageSize);
//        List<SdlInstitutionQaqEntity> list = baseDao.getInstitutionQaqList(params);
//        PageInfo<SdlInstitutionQaqEntity> pageInfo = new PageInfo<>(list);
//
//        return pageInfo;
//    }
}